<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style>
        html,
        body {
            height: 100%;
        }

        form {
            width: 400px;
            position: fixed;
            left: 6%;
            top: 20%;
            box-shadow: 0 0 5px #ccc;
            background-color: rgba(250, 250, 250, 0.4);
        }
    </style>
</head>

<body>
    <!-- 背景轮播图 -->
    <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="./img/login_bg1.jpg" class="d-block w-100" alt="...">
            </div>
            <div class="carousel-item">
                <img src="./img/login_bg2.jpg" class="d-block w-100" alt="...">
            </div>
        </div>
    </div>

    <form class="rounded p-4">
        <h3 class="text-center py-2">注册</h3>
        <div class="form-group my-4">
            <label>用户名</label>
            <input type="text" class="form-control" name="username" />
            <small id="toastId1" class="form-text text-danger" style="display: none;">请输入用户名</small>
        </div>
        <div class="form-group py-2">
            <label>密码</label>
            <input type="password" class="form-control" name="password" />
            <small id="toastId2" class="form-text text-danger"
                style="display: none;">请输入的密码在6-18位且必须包含数字、字母、特殊字符</small>
        </div>
        <div class="d-flex justify-content-center">
            <input type="button" class="btn btn-secondary bg-primary px-5 my-3" id="btnId" value="注册" />
        </div>

    </form>
</body>

</html>
<script src="js/jquery3.6.0_min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<script>

    $(function () {
        $("#btnId").click(function () {
            // console.log($("input[name=username]").val());
            var username = $("input[name=username]").val();
            var password = $("input[name=password]").val();

            if (username == "" && password == "") {
                $("#toastId1").show();
                $("#toastId2").show();
            } else if (username == "") {
                $("#toastId1").show();
            } else if (password == "") {
                $("#toastId2").show();
            } else {
                $("#toastId1").hide();
                // 密码正则判断
                checkPassWord(password);

            }

        })
    })

    // 密码格式：必须包含数字、字母、特殊字符、长度在6-18个字符
    function checkPassWord(password) {

        // 1.获取用户输入的内容
        var str = password
        // 2.逻辑 
        // 2.1长度在6-18个字符之间
        console.log(str.length);
        // 2.2包含数字
        var reg = /\d/;
        var hasNum = false; //通过布尔值判断
        if (reg.test(str)) {
            hasNum = true;
        }
        // 2.3包含字母
        var reg = /[a-z]/gi;
        var hasLetter = false;
        if (reg.test(str)) {
            hasLetter = true;
        }
        // 2.4特殊字符
        var arr = ["$", "@", "#", "&", "%", "|"];
        var reg = /[\$@#&%\|]/;
        var hasSpec = false;
        if (reg.test(str)) {
            hasSpec = true;
        }
        console.log(hasNum, hasLetter, hasSpec)
        // 3.输出结果
        if (hasNum && hasLetter && hasSpec && str.length >= 6 && str.length <= 18) {
            $("#toastId2").hide();
            // 密码符合规则，进行注册
            console.log($("form").serialize());
            resister();
        } else {
            $("#toastId2").show();
        }
    }

    // 注册ajax请求
    function resister() {
        $.ajax({
            type: "POST",
            url: "http://localhost:3000/register",
            data: $("form").serialize(),
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    location.href = "login.html";
                } else if (res.code == 201) {
                    $("#toastId2").show();
                    $("#toastId2").text(res.message);
                }

            },
            error: function (err) {
                console.log(err);
            }
        })
    }

</script>